#include "maualform.h"
#include "ui_maualform.h"
#include "urdfwidget.h"

#include <models/robotlist.h>
#include <mainwindow.h>
#include <models/operatelogmodel.h>
MaualForm::MaualForm(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::MaualForm)
    ,m_pButtonGroup(new QButtonGroup(this))
{
    ui->setupUi(this);
   // ui->data_widget->setupUI();
    ui->head_btn->setChecked(true);//默认选择头部
    m_pButtonGroup->setExclusive(true);  // 设置互斥
    m_pButtonGroup->addButton(ui->head_btn,HEAD_JOINT);
    m_pButtonGroup->addButton(ui->waist_btn,WAIST_JOINT);
    m_pButtonGroup->addButton(ui->left_arm_btn,LEFT_ARM_JOINT);
    m_pButtonGroup->addButton(ui->right_arm_btn,RIGHT_ARM_JOINT);
    m_pButtonGroup->addButton(ui->left_tip_btn,LEFT_TIP_JOINT);
    m_pButtonGroup->addButton(ui->right_tip_btn,RIGHT_TIP_JOINT);
    m_pButtonGroup->addButton(ui->left_tool_btn,LEFT_TOOL_JOINT);
    m_pButtonGroup->addButton(ui->right_tool_btn,RIGHT_TOOL_JOINT);
    connect(m_pButtonGroup,SIGNAL(buttonToggled(int,bool)), this, SLOT(onSwitchJoin(int,bool)));
    ui->chart_widget->setFixedHeight(ui->chart_widget->height() * g_yScale);
    ui->data_widget->setFixedHeight(ui->data_widget->height() * g_yScale);
    ui->control_widget->setFixedHeight(ui->control_widget->height() * g_yScale);
}

MaualForm::~MaualForm()
{
    delete ui;
}

void MaualForm::initTool()
{
    Robot* pCurRobot=RobotList::getInstance()->getCurRobot();
    if(pCurRobot)
    {
        ui->left_tool_btn->setVisible(true);
        switch (pCurRobot->leftTool()) {
        case TOOL_NONE:
            ui->left_tool_btn->setVisible(false);
            break;
        case TOOL_HAND:
            ui->left_tool_btn->setText("左灵巧手");
            break;
        case TOOL_GRIPPER:
            ui->left_tool_btn->setText("左夹爪");
            break;
        case TOOL_SUCTION_CUP:
            ui->left_tool_btn->setText("左吸盘");
            break;
        default:
            break;
        }
        ui->right_tool_btn->setVisible(true);
        switch (pCurRobot->rightTool()) {
        case TOOL_NONE:
            ui->right_tool_btn->setVisible(false);
            break;
        case TOOL_HAND:
            ui->right_tool_btn->setText("右灵巧手");
            break;
        case TOOL_GRIPPER:
            ui->right_tool_btn->setText("右夹爪");
            break;
        case TOOL_SUCTION_CUP:
            ui->right_tool_btn->setText("右吸盘");
            break;
        default:
            break;
        }
    }
}

void MaualForm::onSwitchJoin(int nID, bool b)
{
    //响应切换头部
    if(b)
    {
        m_curJointType=(JointType)nID;
        Robot* pCurRobot=RobotList::getInstance()->getCurRobot();
        QString strTab;
        switch (m_curJointType) {
        case HEAD_JOINT:
            strTab = "切换到头控制";
            break;
        case WAIST_JOINT:
            strTab = "切换到腰控制";
            break;
        case LEFT_ARM_JOINT:
            strTab = "切换到左臂控制";
            break;
        case RIGHT_ARM_JOINT:
            strTab = "切换到右臂控制";
            break;
        case LEFT_TOOL_JOINT:
            strTab = "切换到左工具控制";
            if(pCurRobot)
            {
                switch (pCurRobot->leftTool()) {
                case TOOL_HAND:
                    m_curJointType=LEFT_TOOL_HAND_JOINT;
                    break;
                case TOOL_GRIPPER:
                    m_curJointType=LEFT_TOOL_GRIPPER_JOINT;
                    break;
                case TOOL_SUCTION_CUP:
                    m_curJointType=LEFT_TOOL_SUCKER_JOINT;
                    break;
                default:
                    break;
                }
            }
            break;
        case RIGHT_TOOL_JOINT:

            strTab = "切换到右工具控制";
            if(pCurRobot)
            {
                switch (pCurRobot->rightTool()) {
                case TOOL_HAND:
                    m_curJointType=RIGHT_TOOL_HAND_JOINT;
                    break;
                case TOOL_GRIPPER:
                    m_curJointType=RIGHT_TOOL_GRIPPER_JOINT;
                    break;
                case TOOL_SUCTION_CUP:
                    m_curJointType=RIGHT_TOOL_SUCKER_JOINT;
                    break;
                default:
                    break;
                }
            }
            break;
        case LEFT_TIP_JOINT:
            strTab = "切换到左末端控制";
            break;
        case RIGHT_TIP_JOINT:
            strTab = "切换到右末端控制";
            break;
        default:
            break;
        }
        OPERATE_LOG(strTab);
        ui->control_widget->setJointCount(m_curJointType);
        ui->data_widget->setJointType(m_curJointType);
        ui->chart_widget->setupCharts(m_curJointType);
    }
}

void MaualForm::showEvent(QShowEvent *event)
{
    Q_UNUSED(event);
    initTool();
    //ui->control_widget->setJointCount(m_curJointType);
    onSwitchJoin(HEAD_JOINT,true);//默认选择头部
    ui->head_btn->setChecked(true);
    UrdfWidget * pUrdfWidget = MainWindow::getInstance()->pUrdfWidget();
    pUrdfWidget->setParent(ui->robot_widget);
    pUrdfWidget->show();
    pUrdfWidget->setGeometry(0,0,ui->robot_widget->width(),ui->robot_widget->height());
    pUrdfWidget->lower();
}




